package com.ming_za.sokoban.block;

import com.ming_za.sokoban.item.SokobanItem;
import com.ming_za.sokoban.Sokoban;
import com.ming_za.sokoban.group.SokobanGroup;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.Supplier;

public class RegisterBlock {

    //方块延迟注册器
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Sokoban.MOD_ID);

    //注册方块========================================

    //箱子方块
    public static final RegistryObject<Block> BOX_BLOCK = registryBlock("box_block", () -> new BoxBlock(BlockBehaviour.Properties.of(Material.METAL)
            .strength(3F).requiresCorrectToolForDrops()), SokobanGroup.SOKOBAN_TAB);
    //终点方块
    public static final RegistryObject<Block> END_BLOCK = registryBlock("end_block", () -> new EndBlock(BlockBehaviour.Properties.of(Material.METAL)
            .strength(3F).requiresCorrectToolForDrops()), SokobanGroup.SOKOBAN_TAB);

    //关联物品========================================
    private static <T extends Block> RegistryObject<T> registryBlock(String name, Supplier<T> block, CreativeModeTab tab){

        //注册对象
        RegistryObject<T> toReturn = BLOCKS.register(name,block);

        registryItem(name,toReturn,tab);
        return toReturn;
    }

    //注册方块物品
    private static <T extends Block> RegistryObject<Item> registryItem(String name,RegistryObject<T> block,CreativeModeTab tab){
        return SokobanItem.ITEMS.register(name, () -> new BlockItem(block.get(),new Item.Properties().tab(tab)));
    }
}
